package sunwul.hospital.hosp.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import sunwul.hospital.client.DictFeignClient;
import sunwul.hospital.common.result.Result;
import sunwul.hospital.hosp.mapper.BookingRuleMapper;
import sunwul.hospital.hosp.service.HospitalService;
import sunwul.hospital.model.model.hosp.BookingRule;
import sunwul.hospital.model.model.hosp.Hospital;
import sunwul.hospital.model.vo.hosp.HospitalQueryVo;

import java.util.List;

/**
 * @author sunwul
 * @date 2023/11/6 22:30:27
 * @description
 */
@Api(tags = "医院管理")
@CrossOrigin
@RestController
@RequestMapping("admin/hosp/hospital")
public class HospitalController {

    @Autowired
    HospitalService hospitalService;

    @Autowired
    DictFeignClient dictFeignClient;

    @Autowired
    BookingRuleMapper bookingRuleMapper;

    @ApiOperation(value = "条件与分页查询医院列表")
    @PostMapping("list/{current}/{limit}")
    public Result hospitalByPage(@PathVariable long current, @PathVariable long limit, @RequestBody HospitalQueryVo hospitalQueryVo){
        Page<Hospital> hospitalPage = hospitalService.query()
                .eq((!StringUtils.isEmpty(hospitalQueryVo.getHoscode())), "hoscode", hospitalQueryVo.getHoscode())
                .like((!StringUtils.isEmpty(hospitalQueryVo.getHoscode())), "hosname", hospitalQueryVo.getHosname())
                .page(new Page<>(current, limit));
        List<Hospital> hospitalList = hospitalPage.getRecords();
        hospitalList.parallelStream().forEach(item -> {
            // 通过FeignClient获取字典名称
            // 医院等级
            Result HospitalLevel_dict_res = dictFeignClient.getName("HospitalLevel",item.getHostype());
            String typeName = (String) HospitalLevel_dict_res.getData();
            item.getParam().put("hostypeName", typeName);
            // 省市区
            Result Province_dict_res = dictFeignClient.getName("Province",item.getProvinceCode());
            String province = (String) Province_dict_res.getData();
            item.getParam().put("provinceName", province);
            Result City_dict_res = dictFeignClient.getName("City",item.getCityCode());
            String city = (String) City_dict_res.getData();
            item.getParam().put("cityName", city);
            Result District_dict_res = dictFeignClient.getName("District",item.getDistrictCode());
            String district = (String) District_dict_res.getData();
            item.getParam().put("districtName", district);
        });

        // 将logo,描述,路线置空
        if (hospitalPage.getTotal() > 0) {
            hospitalPage.getRecords().forEach(t -> t.setLogoData(""));
            hospitalPage.getRecords().forEach(t -> t.setIntro(""));
            hospitalPage.getRecords().forEach(t -> t.setRoute(""));
        }
        return Result.ok(hospitalPage);
    }

    @ApiOperation(value = "更新上线状态")
    @GetMapping("updateStatus/{id}/{status}")
    public Result lock(
            @ApiParam(name = "id", value = "医院id", required = true)
            @PathVariable("id") String id,
            @ApiParam(name = "status", value = "状态（0：未上线 1：已上线）", required = true)
            @PathVariable("status") Integer status){
        UpdateWrapper<Hospital> wrapper = new UpdateWrapper<>();
        wrapper.eq("id",id);
        wrapper.set("status",status);
        boolean flag = hospitalService.update(wrapper);
        if (!flag) {
            return Result.fail();
        }
        return Result.ok();
    }

    @ApiOperation(value = "根据ID查询医院详细信息")
    @GetMapping("showHospDetail/{id}")
    public Result showHospDetail(@ApiParam(name = "id", value = "医院id", required = true)
                                     @PathVariable("id") String id){
        Hospital hospital = hospitalService.getById(id);
        // TODO 查询预约信息, 实际生产中,不应当在controller层引入dao层依赖, 此处只是为了方便
        BookingRule bookingRule = bookingRuleMapper.selectOne(new QueryWrapper<BookingRule>().eq("hoscode",hospital.getHoscode()));
        hospital.setBookingRule(bookingRule);
        return Result.ok(hospital);

    }

}
